#ifndef _TERRAIN_H_
#define _TERRAIN_H_

#include "../draw/DrawObject.h"

#include <vector>
using namespace std;

#define WATER	0
#define START	9

#define GET_COLOR(i)\
switch(i) { \
	case WATER:	glColor4d(0,0,1,1); break;\
	case START:	glColor4d(1,0,0,1); break;\
	default:		glColor4d(0,i/8.0,0,1); break; }

typedef vector<vector<int> > terrainVec;

struct Pad
{
	unsigned long x, y;
	int *A, *B, *C, *D;
	
	fVec3D getNormal(unsigned long X, unsigned long Y) const;
//	fVec2D getGradient(unsigned long X, unsigned long Y) const;
	unsigned long getHeight(unsigned long X, unsigned long Y) const;
	bool testIntersection(const fVec3D& orig, const fVec3D& dir, lVec3D& intersection) const;
	void draw();
	void set(unsigned long X, unsigned long Y, int* a, int* b, int* c, int* d) {
		x = X;	y = Y;	A = a;	B = b;	C = c;	D = d;
	}
	
	Pad()	{	set((unsigned long)-1, (unsigned long)-1, NULL, NULL, NULL, NULL);	}
	
	lVec3D getPointA() const;
	lVec3D getPointB() const;
	lVec3D getPointC() const;
	lVec3D getPointD() const;
};

typedef vector<vector<Pad> > PadGrid;

class Terrain: public ModelObject {
	PadGrid pads;
	terrainVec *terrain;
public:
	Terrain(): pads(), terrain(NULL)	{}
	~Terrain();
	
	fVec3D getNormal(unsigned long X, unsigned long Y) const;
//	fVec2D getGradient(unsigned long X, unsigned long Y) const;
	
	void setTerrain(terrainVec* t);
	bool traceRayToMap(const fVec3D& p, const fVec3D& dir, lVec3D& where);
	
	void draw(unsigned long);
	unsigned long getHeight(unsigned long X, unsigned long Y) const;
};

#endif
